<template>
  <el-dialog title="编辑用户信息" :visible.sync="syncDialogVisible" :close-on-click-modal="false" :close-on-press-escape="false"
    width="442px" @open="open">
    <!-- 添加用户头像显示 -->
    <!-- <img :src="userAvatar" alt="用户头像" class="avatar"> -->
    <el-form ref="form" :model="form" :rules="rules" :inline="true" size="medium" hide-required-asterisk>
      <el-form-item label="用户名称：" prop="username">
        <el-input v-model="form.username" autocomplete="off" placeholder="请输入用户名称" maxlength="10" show-word-limit
          class="dialog-input"></el-input>
      </el-form-item>
      <el-form-item label="个性签名：" prop="signature">
        <el-input v-model="form.signature" autocomplete="off" placeholder="请输入个性签名" maxlength="16" show-word-limit
          class="dialog-input"></el-input>
      </el-form-item>
      <el-form-item label="个人简介：" prop="introduction">
        <el-input v-model="form.introduction" autocomplete="off" placeholder="请输入个人简介" maxlength="16" show-word-limit
          class="dialog-input"></el-input>
      </el-form-item>
      <el-form-item label="性别：" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio v-for="(item, index) in stateList" :key="index" :label="item.label">{{ item.value }}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="warning" class="el-button--no-icon" @click="syncDialogVisible = false">取消</el-button>
      <el-button type="success" class="el-button--no-icon" @click="ensureDialog">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateData } from '@/api/user-auth'

export default {
  name: 'ChannelEditDialog',
  props: ['dialogVisible', 'channel'],
  data() {
    return {
      // formLabelWidth: '80px',
      form: {
        userId: 0,
        username: '',
        userImage: '',
        bookiPhone: '',
        signature: '',
        introduction: '',
        sex: '',
        status: ''
      },
      rules: {
        username: [{ required: true, message: '请输入', trigger: 'change' }],
        introduction: [{ required: true, message: '请输入', trigger: 'change' }],
        status: [{ required: true, message: '请选择', trigger: 'change' }]
      },
      stateList: [
        { label: 1, value: '男' },
        { label: 0, value: '女' }
      ],
      // 添加用于存储用户头像路径的变量
      userAvatar: 'path/to/avatar.jpg'
    }
  },
  computed: {
    syncDialogVisible: {
      get() {
        return this.dialogVisible
      },
      set(val) {
        this.$emit('update:dialogVisible', val)
      }
    }
  },
  methods: {
    open: function () {
      this.form = {
        userId: this.channel.userId,
        username: this.channel.username,
        userImage: this.channel.userImage,
        bookiPhone: this.channel.bookiPhone,
        signature: this.channel.signature,
        introduction: this.channel.introduction,
        sex: this.channel.sex,
        // description: this.channel.description,
        status: this.channel.status ? 1 : 0,
        // ord: this.channel.ord
      }
    },
    ensureDialog: async function () {
      try {
        await this.$refs.form.validate()

        const res = await updateData(this.form)
        if (res.code === 200) {
          this.syncDialogVisible = false
          this.$emit('refreshList')
          this.$message({ type: 'success', message: '操作成功！' })
        } else {
          this.$message({ type: 'error', message: res.errorMessage })
        }
      } catch (err) {
        console.log('err: ' + err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/scss/element-variables.scss';

// TODO: 这里再想想
::v-deep .el-dialog__body {
  padding: 30px;
}

.el-form-item {
  margin-bottom: 20px;
}

::v-deep .el-form-item__label {
  color: $--color-text-primary;
}

::v-deep .el-form-item__content {
  color: $--color-text-primary;

  .el-input .el-input__count .el-input__count-inner {
    background: $--background-color-base;
  }
}

 .avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 20px;
}
</style>
